import java.util.HashSet;
import java.util.Set;

public class Solution817 {

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public int numComponents(ListNode head, int[] nums) {
        Set<Integer> set=new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }
        int ans=0,cur=0;
        while (head!=null){
            if (set.contains(head.val)){
                if (cur==0){
                    ans++;
                }
                cur++;
            }else{
                cur=0;
            }
            head=head.next;
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution817().numComponents(new ListNode(0,new ListNode(1,new ListNode(2,new ListNode(3)))),new int[]{0,1,3}));
    }
}
